package com.example.mobile2021606137xzt0618;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private RecyclerView rvMainUsers;
    private Button btnMainInsert;
    private List<Users> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        list = new MyDataAdapter().QueryAll(this);
        rvMainUsers.setAdapter(new UsersRViewAdapter(list));
        btnMainInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_instert,null);
                new AlertDialog.Builder(MainActivity.this).setTitle("添加数据").setView(view).setPositiveButton("添加", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = ((EditText)view.findViewById(R.id.edtTextDialogName)).getText().toString().trim();
                        int age = Integer.valueOf(((EditText)view.findViewById(R.id.edtTextDialogAge)).getText().toString().trim());
                        float score = Float.valueOf(((EditText)view.findViewById(R.id.edtTextDialogScore)).getText().toString().trim());

                        new MyDataAdapter().insert(MainActivity.this,new Users(name,age,score));

                        list.clear();
                       list.addAll(new MyDataAdapter().QueryAll(MainActivity.this));
                        rvMainUsers.getAdapter().notifyDataSetChanged();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        rvMainUsers = (RecyclerView) findViewById(R.id.rvMainUsers);
        btnMainInsert = (Button) findViewById(R.id.btnMainInsert);
        rvMainUsers.setLayoutManager(new LinearLayoutManager(this));
    }
}